#!/bin/sh
#Copyright (C) 2008-2009 Spark Zheng
#
#file:  reborn/reborn.sh
#usage: ./reborn.sh [parameters]
#desc:	transplant my work-environment to other systems
#	   Execute it after you had make it clearly.
#author:	Spark Zheng(mailto: mwishes AT gmail DOT com)
#history:
#		   version 0.0.1   (initialized by Spark at 2008-12-31)
#			version 0.0.2	(2009-04-24, modify some directories)
#			version 0.0.4	(2009-07-16, add sys-packages)
#

DEBUG=3							 #debug level: 0(none)/1/2/3(details)
LOG_FILE=/tmp/$0.${DEBUG}.log	   #debug log file
T_PWD=$(pwd)
TMP_DIR=/tmp/reborn				 #tmp data dir

PATH=$PATH:/usr/lib/news/bin:/sbin:/usr/sbin:/usr/local/sbin

declare -i LEVEL

LEVEL=2
DIST_USER=sparkzheng
DIST_HOME=/home/${DIST_USER}
VIF_FILE=./data/vif_sparkzheng.tar.bz2
ALL_FILE=./data/all_sparkzheng.tar.bz2

DATA_DIR=/media/1AA0C1CDA0C1B015				#my u-disk root dir

#function area begin

#name: usage
function usage()
{
	echo "USAGE:$0 [option]"
	echo "	  -l reborn level: could be 1(build dirs),2(1+build envs),3(2+copy vif),4(3+copy all-files),"
	echo "		 5(tune os), 6(install some package), 7(sync all sort files). Default: 2"
	echo "	  -u user name of reborning system. Default: sparkzheng"
	echo "	  -d dist HOME dir. Default: /home/sparkzheng"
	echo "	  -D accumulating-file dir. Default is /media/1AA0C1CDA0C1B015"
	echo "	  -f vif-backup file. Default: ./vif_sparkzheng.tar.bz2"
	echo "	  -F all-bakcup file. Default: ./all_sparkzheng.tar.bz2"
	echo "	  -h show this message"

	return 0
}

#name: build_dirs
#args: build_dirs dist_dir
#
#backup	 backup fils
#code	   my code dir				 *
#Documents  document dir				*
#Download   download file dir		   *
#ebook	  my ebook dir				*
#english	english data dir
#git		git project from network
#mm		 my love's dir
#Music	  some music
#Pictures   picture dir				 *
#Projects   my projects dir
#release	my projects-release file dir
#research   research files			  *
#source	 the source code of tools	*
#svn		svn project from network
#tmp		tmp files
#tools	  my tools dir
#Videos	 videos dir
#Works		earlier works files
function build_dirs()
{
	cd $1

	[ $DEBUG -ge 1 ] && echo "$0 build_dirs begin($1) at $(date +%c)..." >> $LOG_FILE

	mkdir -p $TMP_DIR

	mkdir -p backup cvs english mm release research tmp Videos git Music Projects svn Works
	mkdir -p code/{algorithm,asm,c,common,cpp,erlang,java,lisp,other,perl,php,python,shell}
	mkdir -p Documents/{blogs,bookmarks,log,my_docs,my_reports,my_sysfile,my_work,other,resume,tech}
	mkdir -p Download/{datastruct,devel,dist,dstor,fs,game,kernel,language}
	mkdir -p Download/{lib,mobile,module,net,old,other,security,storage,tmp,utils,win,work}
	mkdir -p ebook/{algorithm_math,architecture,c,cold,cpp,dstor,fs}
	mkdir -p ebook/{intel,kernel,language,linux,program,storage,web_network}
	mkdir -p Pictures/{baby,backgroud,dear,family,famous,friend,google,interest,linux,mm,tec,vip,work,yhzh}
	mkdir -p source/{dbm,devel,dstor,game,gtk,lib,media,network,qt,storage,utility}
	mkdir -p tools/{bin,sbin}

	[ $DEBUG -ge 1 ] && echo "$0 build_dirs finish at $(date +%c)." >> $LOG_FILE

	cd $T_PWD

	return 0
}

#name: build_envs
#args: build_envs home_dir policy(0,1)
#
function build_envs()
{
	[ $DEBUG -ge 1 ] && echo "$0 build_envs begin($1:$2) at $(date +%c)..." >> $LOG_FILE

	if [ -e files/bash_rc ]
	then
		cp -f $1/.bashrc $TMP_DIR
		cp -f files/bash_rc $1/.bash_rc
		sed -i "s#/etc/bashrc#$1/.bash_rc#g" $1/.bashrc

		[ $DEBUG -ge 3 ] && echo "$0 setup bash_rc over." >> $LOG_FILE
	fi

	if [ -e files/vim_rc ]
	then
		[ -e $1/.vimrc ] &&  cp -f $1/.vimrc $TMP_DIR
		cp -f files/vim_rc $1/.vimrc

		[ $DEBUG -ge 3 ] && echo "$0 setup vim_rc over." >> $LOG_FILE
	fi

	if [ -e files/vim_plugin.tar.bz2 ]
	then
		[ -d $1/.vim ] &&  cp -rf $1/.vim $TMP_DIR
		tar -C $1 -vxjf files/vim_plugin.tar.bz2

		[ $DEBUG -ge 3 ] && echo "$0 setup vim_plugin over." >> $LOG_FILE
	fi

	if [ -e files/emacs_rc ]
	then
		[ -e $1/.emacs ] && cp -f $1/.emacs $TMP_DIR
		cp -f files/emacs_rc $1/.emacs

		[ $DEBUG -ge 3 ] && echo "$0 setup emacs_rc over." >> $LOG_FILE
	fi

	if [ "x$2" == "x1" -a -e files/subversion_rc.tar.bz2 ]
	then
		if [ -d "$1/.subversion" ]
		then
			cp -rf $1/.subversion $TMP_DIR
		fi

		tar -C $1 -vxjf files/subversion_rc.tar.bz2

		[ $DEBUG -ge 3 ] && echo "$0 setup subversion_rc over." >> $LOG_FILE
	fi

	if [ "x$2" == "x1" -a -e files/ssh_rc.tar.bz2 ]
	then
		if [ -d "$1/.ssh" ]
		then
			cp -rf $1/.ssh $TMP_DIR
			tar -C $1 -vxjf files/ssh_rc.tar.bz2

			[ $DEBUG -ge 3 ] && echo "$0 setup ssh_rc over." >> $LOG_FILE
		fi
	fi

	if [ -d files/bin ]
	then
		cp -rf files/bin/* $1/tools/bin/

		[ $DEBUG -ge 3 ] && echo "$0 setup special bins over." >> $LOG_FILE
	fi

	if [ -d files/sbin ]
	then
		cp -rf files/sbin/* $1/tools/sbin/

		[ $DEBUG -ge 3 ] && echo "$0 setup special sbins over." >> $LOG_FILE
	fi

	[ $DEBUG -ge 1 ] && echo "$0 build_envs finish at $(date +%c)." >> $LOG_FILE

	return 0
}

#name: tune_os
#args: tune_os home_dir
#   filesystem?swap?hdparm?sysctl?
#   Need root privilege
#/etc/ld.so.conf
#/etc/man.config
#/etc/yum.repos.d/*
#/etc/sysctl.conf
#/etc/hosts
#/etc/resolv.conf
#/var/spool/cron/root
#/etc/crond.d(crond.daily...)
#chkconfig --list
#
# sysctl:
#net.ipv4.ip_forward = 0
#net.ipv4.conf.default.rp_filter = 1
#net.ipv4.conf.default.accept_source_route = 0
#kernel.sysrq = 0
#kernel.core_uses_pid = 1
#net.ipv4.tcp_syncookies = 1
#kernel.msgmnb = 65536
#kernel.msgmax = 65536
#kernel.shmmax = 68719476736
#kernel.shmall = 4294967296
#kernel.panic = 1
#net.ipv4.tcp_rmem = 8192	873800	8738000
#net.ipv4.tcp_wmem = 4096	655360	6553600
#net.ipv4.ip_local_port_range = 1024	65000
#net.core.rmem_max = 16777216
#net.core.wmem_max = 16777216
#
function tune_os()
{
	if [ $UID -ne 0 ]
	then
		echo "Tune_os need root privilege."
		return 253
	fi

	[ $DEBUG -ge 1 ] && echo "$0 tune_os begin($1) at $(date +%c)..." >> $LOG_FILE

	cp -f /etc/ld.so.conf $TMP_DIR
	cp -f /etc/man.config $TMP_DIR
	cp -rf /etc/yum.repos.d $TMP_DIR
	cp -f /etc/sysctl.conf $TMP_DIR
	cp -f /etc/hosts $TMP_DIR
	cp -f /etc/resolv.conf $TMP_DIR
	cp -f /var/spool/cron/root $TMP_DIR

	echo "/usr/local/lib" >> /etc/ld.so.conf
	echo "$1/tools/lib" >> /etc/ld.so.conf
	if [ $? -eq 0 ]
	then
		[ $DEBUG -ge 3 ] && echo "$0 tune up ld.so.conf over." >> $LOG_FILE
	fi

	sed -i "/^MANPATH   /{: a
							   n
							   /^MANPATH	/ b a
		i \MANPATH  ${1}/tools/share/man
							}" /etc/man.config
	sed -i "/^MANPATH_MAP   /{: a
								   n
								   /^MANPATH_MAP	/ b a
		i \MANPATH_MAP  ${1}/tools/bin	${1}/tools/share/man\nMANPATH_MAP   ${1}/tools/sbin	${1}/tools/share/man
								}" /etc/man.config
	if [ $? -eq 0 ]
	then
		[ $DEBUG -ge 3 ] && echo "$0 tune up man.config over." >> $LOG_FILE
	fi

	cat >> /etc/sysctl.conf <<EOF
#
# More active applications
vm.swappiness = 10

# Controls TCP TIME_WAIT queue
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_max_tw_buckets = 262144

# Half-SYNC DDOS defence
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_max_orphans = 262144
#somaxconn ESTABLISHED listen-backlog max size
net.core.somaxconn = 262144
net.core.netdev_max_backlog = 262144
#syn_backlog SYNC_RECV queue max size
net.ipv4.tcp_max_syn_backlog = 8192

# network
net.ipv4.tcp_keepalive_time = 1800
net.ipv4.ip_local_port_range = 5000 65000
net.ipv4.tcp_timestamps = 1

# network for big file and big memory
net.ipv4.tcp_rmem = 8192 4194304 8388608
net.ipv4.tcp_wmem = 4096 2097152 8388608
net.core.rmem_max = 8388608
net.core.rmem_default = 4194304
net.core.wmem_max = 8388608
net.core.wmem_default = 2097152

EOF
	if [ $? -eq 0 ]
	then
		[ $DEBUG -ge 3 ] && echo "$0 tune up sysctl over." >> $LOG_FILE
	fi

	cat >> /etc/hosts <<EOF
#
## Home ##################
192.168.0.6 fatlism fatlism
192.168.0.5	hzc	hzc
192.168.0.4 mm	mm

192.168.0.3	www.sparkzheng.com
##########################

###ultravpn
87.98.173.225   servers443.ultravpn.net
#######################################

EOF
	if [ $? -eq 0 ]
	then
		[ $DEBUG -ge 3 ] && echo "$0 tune up hosts over." >> $LOG_FILE
	fi

	cat >> /var/spool/cron/root <<EOF
#
###every day to execute updatedb since mlocate was got rid of cron.daily###
0 13 * * * /bin/bash ${1}/tools/sbin/mlocate
###end
EOF
	if [ $? -eq 0 ]
	then
		[ $DEBUG -ge 3 ] && echo "$0 tune up crontab for root over." >> $LOG_FILE
	fi

	[ $DEBUG -ge 1 ] && echo "$0 tune_os finish at $(date +%c)." >> $LOG_FILE

	return 0
}

#name: install_necessary
#args:
#		need root privillege
#		install extras yum repository
#		rpmfusion-free-release-stable.noarch.rpm
#		rpmfusion-nonfree-release-stable.noarch.rpm
#		livna-release.rpm
#
#	yum-plugin-fastestmirror:
#	kernel-headers:
#	kernel-devel:
#	glibc-debuginfo,glibc-utils,glibc-static,cflow:
#	llvm,llvm-devel,llvm-doc:
#	iostat,sysstat,iperf,iptraf,tcptrace:
#	vlc,mencoder,mplayer,smplayer,dvbcut,ffmpeg,ffmpeg-devel,ffmpeg-libs,kmplayer,xmms2-avcodec
#	xdtv,gstreamer,gstreamer-ffmpeg,xine,xine-lib,xine-lib-extras-freeworld,cheese:
#	wine,winetools,sconj:
#   libicu,pcre,pcre-devel,pcre-static,js,js-devel,js-static:
#	virtualbox,bochs,vmware,busybox,libvirt,libvirt-python:
#	bonnie++,iozone:
#	anjuta,geany,geany-plugins,codeblocks,codeblocks-contrib:
#	google-perftools,google-perftools-devel,online-desktop-google-docs,online-desktop-google-reader:
#   protobuf,protobuf-devel,protobuf-python,protobuf-static,protobuf-vim:
#	gnuplot,libatomic:
#   opencv-devel,opencv-python,python-lockfile:
#	lifera,wireshark,adobe reader,p7zip,backintime,stardict,cherokee,dnsmasq,dropbear,gearman,chmsee:
#	varnish,haproxy,keepalived,nginx,collectd,lighttpd,lighttpd-fastcgi:
#   heartbeat,heartbeat-devel,heartbeat-gui,haproxy,drbd:
#   fcgi,fcgi-devel,spawn-fcgi:
#   bleachbit:
#	guile(for scheme):
#	twisted-python,twisted-python-core,python-lxml,pexpect,libsvm-python,libxml2-python,wxPython:
#	MySQL-python,python-pgsql,python-Levenshtein,ipython,Django:
#	erlang,erlang-doc,erlang-erlsyslog:
#	git,git-all,git-svn,git-gui,mercurial:
#	doxygen,doxygen-wizard,global,ctags,python-ctags:
#	texlive-xetex,kile:
#	libstdc++-docs.i586,cppunit-doc.i586,log4cpp,log4c:
#	libevent,libevent-devel,libev,libev-devel,pyevent:
#	bd4,qdbm,tokyocabinet,db4-devel,db4-cxx,db4-devel-static,db4-utils,qdbm++,qdbm++-devel,qdbm-devel,tokyocabinet-devel:
#   pytc,pytyrant,tokyotyrant,tokyotyrant-devel,tokyotyrant-libs,couchdb,mongo:
#   dbxml,dbxml-devel,dbxml-python,dbxml-utils,dbxml-doc:
#	memcached memcached-selinux libmemcached libmemcached-devel python-memcached:
#	fvwm,tor,privoxy:
#	perl-XML-Simple:
#	kodos:
function install_necessary()
{
	[ $DEBUG -ge 1 ] && echo "$0 install_necessary begin($1:$2) at $(date +%c)..." >> $LOG_FILE
	
	rpm -Uvh  http://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-stable.noarch.rpm
	rpm -Uvh  http://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-stable.noarch.rpm
	rpm -Uvh http://rpm.livna.org/livna-release.rpm

	[ $DEBUG -ge 1 ] && echo "$0 install_necessary need exec yum install *** by hand." >> $LOG_FILE

	[ $DEBUG -ge 1 ] && echo "$0 install_necessary finish at $(date +%c)." >> $LOG_FILE

	return 0
}

#name: render_vif
#args: render_vif dist_dir vif_file
#
function render_vif()
{
	[ $DEBUG -ge 1 ] && echo "$0 render_vif begin($1:$2) at $(date +%c)..." >> $LOG_FILE

	if [ -e $2 ]
	then
		tar -C $1 -vxjf $2

		[ $DEBUG -ge 3 ] && echo "$0 cp vif file to $1 over." >> $LOG_FILE
	fi

	[ $DEBUG -ge 1 ] && echo "$0 render_vif finish at $(date +%c)." >> $LOG_FILE

	return 0
}

#name: render_all
#args: render_all dist_dir all_file
#		
function render_all()
{
	[ $DEBUG -ge 1 ] && echo "$0 render_all begin($1:$2) at $(date +%c)..." >> $LOG_FILE

	if [ -e $2 ]
	then
		tar -C $1 -vxjf $2

		[ $DEBUG -ge 3 ] && echo "$0 cp all files to $1 over." >> $LOG_FILE
	fi

	[ $DEBUG -ge 1 ] && echo "$0 render_all finish at $(date +%c)." >> $LOG_FILE

	return 0
}

#name: render_all_offline
#args: render_all_offline dist_home src_offline_disk_home 
#		
function render_all_offline()
{
	[ $DEBUG -ge 1 ] && echo "$0 render_all_offline begin($1:$2) at $(date +%c)..." >> $LOG_FILE

	#my_code
	[ -d $1/code ] && rsync -au $2/my_code/code $1/
	[ -d $1/Projects ] && rsync -au $2/my_code/Projects  $1/
	[ -d $1/release ] && rsync -au $2/my_code/release $1/
	[ -d $1/research ] && rsync -au $2/my_code/research $1/ 
	[ -d $1/source ] && rsync -au $2/my_code/source $1/ 
	[ -d $1/git ] && rsync -au $2/my_code/git $1/
	[ -d $1/svn ] && rsync -au $2/my_code/svn $1/ 
	[ -d $1/cvs ] && rsync -au $2/my_code/cvs $1/ 

	#my_doc
	[ -d $1/Documents ] && rsync -au $2/my_doc/Documents $1/ 
	[ -d $1/ebook ] && rsync -au $2/my_doc/ebook $1/ 

	#my_music
	[ -d $1/Music ] && rsync -au $2/my_music/Music $1/ 
	[ -d $1/english ] && rsync -au $2/my_music/english $1/
	
	#my_pic
	[ -d $1/Pictures ] && rsync -au $2/my_pic/* $1/Pictures/
	
	#my_video
	#my_work
	
	#tools
	[ -d $1/Download ] && rsync -au $2/tools/Download $1/ 

	[ $DEBUG -ge 1 ] && echo "$0 render_all_offline finish at $(date +%c)." >> $LOG_FILE

	return 0
}

#function area end

#main begin
[ $DEBUG -ge 1 ] && echo "$0 begin at $(date +%c)..." >> $LOG_FILE

#getopt from cmdline
while getopts ":l:u:d:D:f:F:h" options
do
	case $options in
		l)  LEVEL=$OPTARG
			;;
		u)  DIST_USER=$OPTARG
			;;
		d)  DIST_HOME=$OPTARG
			;;
		D)  DATA_DIR=$OPTARG
			;;
		f)  VIF_FILE=$OPTARG
			;;
		F)  ALL_FILE=$OPTARG
			;;
		h)  usage
			exit 255
			;;
		*)  usage
			exit 254
			;;
	esac
done

shift $(($OPTIND - 1))

[ $DEBUG -ge 3 ] && echo "$0 runtime parameters: level: $LEVEL" >> $LOG_FILE
[ $DEBUG -ge 3 ] && echo "$0 runtime parameters: user: $DIST_USER" >> $LOG_FILE
[ $DEBUG -ge 3 ] && echo "$0 runtime parameters: home dir: $DIST_HOME" >> $LOG_FILE
[ $DEBUG -ge 3 ] && echo "$0 runtime parameters: accu-data-dir: $DATA_DIR" >> $LOG_FILE
[ $DEBUG -ge 3 ] && echo "$0 runtime parameters: vif file: $VIF_FILE" >> $LOG_FILE
[ $DEBUG -ge 3 ] && echo "$0 runtime parameters: all file: $ALL_FILE" >> $LOG_FILE
[ $DEBUG -ge 3 -a "x$1" != "x" ] && echo "$0 runtime parameters: other: $1" >> $LOG_FILE


#create dirs on my work-space
if [ $LEVEL -ge 1 ]
then
	build_dirs $DIST_HOME
fi

#setup my working environment
if [ $LEVEL -ge 2 ]
then
	build_envs $DIST_HOME 0
fi

#transport important files from vif-backups
if [ $LEVEL -eq 3 -o $LEVEL -eq 4 ]
then
	render_vif $DIST_HOME $VIF_FILE
fi

#transport all required files from related-backups
if [ $LEVEL -eq 4 ]
then
	render_all $DIST_HOME $ALL_FILE
fi

#tune up operation system parameters
if [ $LEVEL -eq 5 ]
then
	tune_os $DIST_HOME
fi

#install some necessary packages
if [ $LEVEL -eq 6 ]
then
	install_necessary
fi

#rsync all sort from backup-dirs
if [ $LEVEL -eq 7 ]
then
	render_all_offline $DIST_HOME $DATA_DIR
fi

[ $DEBUG -ge 1 ] && echo "$0 finish at $(date +%c)." >> $LOG_FILE
[ $DEBUG -ge 1 ] && echo "---------------------------------------" >> $LOG_FILE

exit 0
#main end
